package org.sy.lab.Java7并发编程实战.七定制并发类.六通过实现ThreadFactory接口为ForkJoin框架生成定制线程;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;

public class MyWorkerThread extends ForkJoinWorkerThread {
	private static ThreadLocal<Integer> taskCounter = new ThreadLocal<Integer>();
	protected MyWorkerThread(ForkJoinPool pool) {
		super(pool);
	}
	@Override
	protected void onStart() {
		super.onStart();
		System.out.printf("MyWorkerTherad %d: Initializing task counter.\n", getId());
		taskCounter.set(0);
	}
	@Override
	protected void onTermination(Throwable exception) {
		System.out.printf("MyWorkerThread %d: %d\n", getId(), taskCounter.get());
		super.onTermination(exception);
	}
	public void addTask() {
		int counter = taskCounter.get().intValue();
		counter++;
		taskCounter.set(counter);
	}
}
